clear all; close all; clc

% IMPORTANT: Please run the script 'RUN_ME_Analyze_data.m' first. 
% This code generates a table with all out-of-sample activation dates across counties.  

%% Choose variables and parameters
alphamin           = 1;       % choose minimum number of positive derivative events for the loop
strategy           = 'fast';  % choose strategy fast vs slow: fast picks up the minimum optimal alpha and threshold values (bottom-left of a rectangle), while slow picks up the maximum optimal values

%% Load data
load vec_county.mat;

% Load weeks
table_weeks = readtable('weeks_new_table.xlsx');
weeks       = char(table2array(table_weeks));
clear table_weeks

% Load Rt data and event weeks
table_events = readtable('data_events_omicron_shifted.xlsx');

vec_var1{1}  = 'JHU_cases';



for ind_var1 = 1:length(vec_var1) % start loop on predictor variables
    var1     = vec_var1{ind_var1}; % set predictor variable
    
    % start loop on counties 
    for ind_county =1:length(vec_county)
        
        % Find events for the county
        aux_ind_county              = contains(string(table_events{:,2}),vec_county(ind_county));
        table_selected_events       = table_events(aux_ind_county,:);
        
        for  ind_last_train_wave = 1:size(table_selected_events,1)-1 % open file training up to wave ind_last_train_wave and testing wave ind_last_train_wave+1
            
             filename = ['result_files_reproducible/Results_omicron_shifted_parag_prob_training_upto_wave_' num2str(ind_last_train_wave) '_test_wave_' num2str(ind_last_train_wave+1) '_alphamin_' num2str(alphamin) '_strategy_' strategy '_signal_' var1 '_ind_county_' num2str(ind_county) '.mat'];
            
            if exist(filename, 'file') == 2
                
                load(filename,'activation_weeks_var1_test')
                
                % Store activation weeks for predictor variable
                for i1=1:length(activation_weeks_var1_test)
                    aux_activation_weeks_var1_test(i1)= activation_weeks_var1_test{i1};
                end
                cell_unique_activation_weeks_var1_test{ind_county,ind_var1,ind_last_train_wave} = unique(aux_activation_weeks_var1_test);
                
                % Find last week of training set
                aux_ind_endweek_train              = datetime(weeks)==table_selected_events{ind_last_train_wave,5};
                endweek_train                      = find(aux_ind_endweek_train);
                
                % Find index of last week in test dataset
                aux_ind_endweek_test                  = datetime(weeks)==table_selected_events{ind_last_train_wave+1,5}; %
                endweek_test                          = find(aux_ind_endweek_test);
                
                % we count activations occuring only between the end of
                % last training wave and end of test wave
                testweek_fortables                    = intersect(cell_unique_activation_weeks_var1_test{ind_county,ind_var1,ind_last_train_wave},endweek_train+1:endweek_test);
                
                % if there are activations in that period, proceed
                if  ~isempty(testweek_fortables)
                    
                    cell_dates_for_tables{ind_county,ind_var1,ind_last_train_wave} = weeks(testweek_fortables,:); % store weeks in 3D cell array
                end
                
                % clear variables for loop in training waves
                clearvars -except ind_last_train_wave data_table alphamin strategy vec_county vec_var1 count var1 ...
                    ind_var1 ind_county cell_unique_activation_weeks_var1_test...
                    table_events table_selected_events weeks cell_dates_for_tables
            end
        end
        
        clear table_selected_events ind_last_train_wave
        
        
    end
    
    clear var1
end


% reset indicator for another loop
clear ind_county ind_var1 ind_last_train_wave 

% set counter for table rows
count= 0;


% start loop to build table for Leo
for ind_var1 = 1:length(vec_var1) % start loop on predictor 
    for ind_county = 1:size(cell_dates_for_tables,1) % start loop on counties
        count = count+1; % update counter 
        
        for ind_last_train_wave =1 % get activation for first wave, if any.
            
            aux_dates = cell_dates_for_tables{ind_county,ind_var1,ind_last_train_wave};
            
            if ~isempty(aux_dates)  % if statement to build list_dates only if there was some activation
                list_dates = aux_dates;
            else 
                list_dates = blanks(1); % if no activation, leave it blank
            end
        end

        for ind_last_train_wave =2:size(cell_dates_for_tables,3) % for the upcoming training waves 
            
            aux_dates = cell_dates_for_tables{ind_county,ind_var1,ind_last_train_wave};
            if ~isempty(aux_dates)
                list_dates = char({list_dates;aux_dates});
            end
        end
        
        data_table(count,:) = {vec_var1(ind_var1) ,vec_county(ind_county),list_dates};
        clear list_dates
    end
end


 table_name       = ['activations_table_naive.csv'];
 result_table = table(data_table);
 writetable(result_table,table_name)



